import 'package:flutter/material.dart';

import '../constants.dart' show Constants;
import '../constants.dart' show AppColors;

import './conversation_page.dart';

enum ActionItems {
  GROUP_CHACT,ADD_FRIEND,QR_SCAN,PAYMENT,HELP
}

class NavigationIconView{
  final String _title;
  final BottomNavigationBarItem item;

  NavigationIconView({Key key, String title,IconData icon,IconData activeIcon}):
  _title=title,
  item = BottomNavigationBarItem(
    icon: Icon(icon),
    activeIcon: Icon(activeIcon) ,
    title: Text(title,style: TextStyle(
      fontSize: 14.0,
      color: Color(AppColors.TabIconNormal)
    ),),
    backgroundColor: Colors.white,
  );
}

class HomeScreen extends StatefulWidget {
 

  final Widget child;

  HomeScreen({Key key, this.child}) : super(key: key);

  _HomeScreenState createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  PageController _pageController;
  int _currentIndex = 0;
  List<NavigationIconView> _navigationViews;
  List<Widget> _pages;

  void initState(){
    super.initState();
    _navigationViews = [
      NavigationIconView(
        title: '微信',
        icon: IconData(
          0xe600,
          fontFamily:Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe61b,
          fontFamily:Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '通讯录',
        icon: IconData(
          0xe602,
          fontFamily:Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe6c2,
          fontFamily:Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '发现',
        icon: IconData(
          0xe609,
          fontFamily:Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe67a,
          fontFamily:Constants.IconFontFamily,
        ),
      ),
      NavigationIconView(
        title: '我',
        icon: IconData(
          0xe60b,
          fontFamily:Constants.IconFontFamily,
        ),
        activeIcon: IconData(
          0xe608,
          fontFamily:Constants.IconFontFamily,
        ),
      ),
    ];
    _pageController =PageController(initialPage: _currentIndex);
    _pages = [
      ConversationPage(),
      Container(color: Colors.green,),
      Container(color: Colors.blue,),
      Container(color: Colors.brown,)
    ];
  }
  
  _buildPopupMunuItem(int iconName,String title){
    return Row(
      children: <Widget>[
        Icon(
          IconData(
            iconName,
            fontFamily:Constants.IconFontFamily,
          ),
          size: 22.0,
          color: Color(AppColors.AppBarPopupMenuColor),
        ),
        Container(width: 12.0,),
        Text(title,style: TextStyle(color:const Color(AppColors.AppBarPopupMenuColor)),),

      ],
    );
  }


  @override
  Widget build(BuildContext context) {
    final BottomNavigationBar botNavBar =BottomNavigationBar(
      fixedColor: const Color(AppColors.TabIconActive),
      items: _navigationViews.map((NavigationIconView view){
             return view.item;
           }).toList(),
           currentIndex: _currentIndex,
           type: BottomNavigationBarType.fixed,
           onTap: (int index){
             setState(() {
               _currentIndex = index;

               _pageController.animateToPage(
                 _currentIndex, 
                 duration:Duration(milliseconds: 200),
                 curve: Curves.easeInOut,
                 );
             });
             print('点击的是第$index个Tab');
           },
    );
    return Scaffold(
      appBar: AppBar(
        title: Text('微信'),
        elevation: 0.0,
        actions:[
          Container(
            padding: const EdgeInsets.only(right: 6.0),
              child: IconButton(
              icon: Icon(Icons.search),
              onPressed: (){print('点击了搜索按钮');},
            ),
          ),
          PopupMenuButton(
            itemBuilder: (BuildContext context){
              return <PopupMenuItem<ActionItems>>[
                PopupMenuItem(
                  child: _buildPopupMunuItem(0xe69e,"发起群聊"),
                  value: ActionItems.GROUP_CHACT,
                ),
                 PopupMenuItem(
                  child: _buildPopupMunuItem(0xe638,"添加朋友"),
                  value: ActionItems.ADD_FRIEND,
                ),
                 PopupMenuItem(
                  child: _buildPopupMunuItem(0xe601,"扫一扫"),
                  value: ActionItems.QR_SCAN,
                ),
                 PopupMenuItem(
                  child: _buildPopupMunuItem(0xe62a,"收付款"),
                  value: ActionItems.PAYMENT,
                ),
                 PopupMenuItem(
                  child: _buildPopupMunuItem(0xe660,"帮助与反馈"),
                  value: ActionItems.HELP,
                ),
              ];
            },
            icon: Icon(Icons.add),
            onSelected: (ActionItems selected){
              print('点击的是$selected');
            },
          ),
          Container(width: 16.0),
        ]
      ),
      body:PageView.builder(
        itemBuilder: (BuildContext constext,int index){
          return _pages[index];
        },
        controller: _pageController,
        itemCount: _pages.length,
        onPageChanged: (int index){
          setState(() {
           _currentIndex =index; 
          });
        },
      ),
      bottomNavigationBar: botNavBar, 
    );
  }
}